/**
 *  Copyright (C) 2006 zhangbo (freeeob@gmail.com)
 *
 *  This product is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 * 
 *  This product is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 *  author:zhangbo
 *  Email:jsjava@gmail.com
 */
 
function ClipboardUtils(){
	this.jsjava_class="jsorg.eob.document.ClipboardUtils";
}

/**
 * Copy text to system clipboard
 * @param data
 */
ClipboardUtils.copyTextToClipboard=function(data){
	if(data==undefined||data==null){
		return;
	}
	if(BrowserUtils.isIE()){
		window.clipboardData.clearData();
		window.clipboardData.setData("text",data);
	}else if(BrowserUtils.isOpera()){
		window.location=data;
	}else if(BrowserUtils.isFirefox()){
		try{  
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
        }catch (e){ 
            throw new SecurityException(SecurityException.ERROR,"");
        }
        var clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);  
        if (!clip){  
            return;  
        }
        var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);  
        if (!trans){  
            return;  
        }
        trans.addDataFlavor("text/unicode");  
        var str = new Object();  
        var len = new Object();  
        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);  
        var copytext = data;  
        str.data = copytext;  
        trans.setTransferData("text/unicode",str,copytext.length*2);  
        var clipid = Components.interfaces.nsIClipboard;  
        if (!clip){  
            return false;  
        }
        clip.setData(trans,null,clipid.kGlobalClipboard); 
	}
	return true;
};

/**
 * Get text from system clipboard
 */
ClipboardUtils.getTextFromClipboard=function(){
	var pastetext;
	if(BrowserUtils.isIE()){
		pastetext=window.clipboardData.getData("text");
	}else if(BrowserUtils.isFirefox()){
		try{  
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
        }catch (e){ 
            throw new SecurityException(SecurityException.ERROR,"");
        }
        var clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);  
        if (!clip){  
            return;  
        }
        var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);  
        if (!trans){  
            return;  
        }
        trans.addDataFlavor("text/unicode");  
        clip.getData(trans,clip.kGlobalClipboard); 
        var str=new Object(); 
        var strLength=new Object(); 
        trans.getTransferData("text/unicode",str,strLength);
        if (str){
        	str=str.value.QueryInterface(Components.interfaces.nsISupportsString); 
        }
        if (str){
        	pastetext=str.data.substring(0,strLength.value / 2); 
        }
	}
	return pastetext;
};